package com.tool;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ParamHandler {

    private ParamHandler() { }

    /**
     * ids解析
     */
    public static List<Long> idsParser(String ids) {
        return idsParser(ids, Long::valueOf);
    }


    /**
     * ids解析
     */
    public static List<Integer> IntegerTypeParser(String ids) {
        return idsParser(ids, Integer::valueOf);
    }

    public static List<String> StringTypeParser(String ids) {
        return idsParser(ids, String::valueOf);
    }

    public static <R>List<R> idsParser(String ids, Function<String, R> fun) {
        if (StringUtils.isBlank(ids)) {
            return Collections.emptyList();
        }
        return Arrays.stream(ids.split(";"))
                .filter(StringUtils::isNotBlank)
                .map(fun)
                .collect(Collectors.toList());
    }

    public static List<Long> idsParserByComma(String ids) {
        return idsParserByComma(ids, Long::valueOf);
    }

    public static List<Integer> IntegerTypeParserByComma(String ids) {
        return idsParserByComma(ids, Integer::valueOf);
    }

    public static <R>List<R> idsParserByComma(String ids, Function<String, R> fun) {
        if (StringUtils.isBlank(ids)) {
            return Collections.emptyList();
        }
        return Arrays.stream(ids.split(","))
                .filter(StringUtils::isNotBlank)
                .map(fun)
                .collect(Collectors.toList());
    }

}
